package edu.mcw.rgd.clinminer.web.userrole;

import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Table;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;

import edu.mcw.rgd.clinminer.web.EntityEditor;
import edu.mcw.rgd.clinminer.web.EntityTableColumnGenerator;
import edu.mcw.rgd.clinminer.web.ui.ParticipantView;

public class ParticipantReportView extends ParticipantView {
	private static final long serialVersionUID = 1L;
	private AbsoluteLayout mainLayout;


	@Override
	protected EntityEditor createForm() {
		return new ParticipantReportForm();
	}

	/**
	 * Return the columns (property ids) to show in the entity table.
	 *
	 * @return array of property ids for the entity type
	 */
	@Override
	public Object[] getTableColumns() {
		String[] props = new String[] { "name", "studies", };
		return props;
	}

	@Override
	public void setupGeneratedColumns(Table table) {
		table.removeGeneratedColumn("studies");
		table.addGeneratedColumn("studies", new EntityTableColumnGenerator(
				"name"));
	}

	@Override
	public void addListeners() {
		getTable().addListener(new ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				Object value = event.getProperty().getValue();
				if (value != null) {
					navigateToFragment("show/"
							+ String.valueOf(value).replaceAll("[^0-9]", ""));
				}
			}
		});
	}

	@Override
	public void navigateTo(String requestedDataId) {
		// refresh table
		refresh();

		setDirty(false);

		if (null == requestedDataId || "".equals(requestedDataId)) {
			getTable().setValue(null);
			// continue
		} else if (requestedDataId.startsWith("show/")) {
			try {
				Long id = Long.valueOf(requestedDataId.substring(5));
				setCurrentEntity(getEntityForItem(getTable().getItem(id)));
				getTable().setValue(id);
				return;
			} catch (NumberFormatException e) {
				navigateToFragment(null);
				return;
			}
		}

		setCurrentEntity(null);
	}

}
